var app = getApp()
//获取应用实例  
Page({
  data: {
    pics: [],
    num: "100",
  },
  //登录检测
  dljc: function () {
    var openid = getApp().globalData.openid;
    var nickname = getApp().globalData.nickName;
    var that = this;
    wx.request({
      url: getApp().globalData.API_URI + '/index.php/face_dljc',
      data: { //appid 与 secret为对应小程序项目后台获取
        'openid': openid,
        'nickname': nickname,
      },
      header: {//请求头  
        "Content-Type": "application/x-www-form-urlencoded"
      },
      method: 'POST',
      success: function (res) {
        console.log(res.data)
        that.setData({ jifen: res.data.jifen, })
        if (res.data.error == "1") {
          that.setData({ text: 2 })
        }
      }
    })
  },
  //弹出积分详情
  jifen_open: function () {
    this.setData({
      text1: "1",
    })
  },
  //关闭积分详情
  jifen_close: function () {
    this.setData({
      text1: "2",
    })
  },

  onShareAppMessage: function () {
    return {
      title: '一键变脸',
      path: 'pages/index/index?openid=' + getApp().globalData.openid,
      success: function (res) {
        // 转发成功
        console.log("转发成功");
      },
      fail: function (res) {
        // 转发失败
        console.log("转发失败");
      }
    }



  },





  onLoad: function (options) {
    // console.log("传过来的是");
    // console.log(options.people_type1);
    // console.log(options.img_url);
    getApp().globalData.img_url = getApp().globalData.API_URI + "/public/upload/weixin1/" + options.img_url;
    getApp().globalData.people_type1 = options.people_type1;
    this.dljc();
    //分享事件
    wx.showShareMenu({
      withShareTicket: true,
    })
    //页面合成的图片
    console.log("页面合成的图片路径" + getApp().globalData.img_url);
    this.change();



    
    //是否播放音乐
    //this.listenerButtonPlay();
    wx.onBackgroundAudioPlay(function () {
      console.log('onBackgroundAudioPlay')
    })

		/**
		 * 监听音乐暂停
		 */
    wx.onBackgroundAudioPause(function () {
      console.log('onBackgroundAudioPause')
    })


		/**
		 * 监听音乐停止
		 */
    wx.onBackgroundAudioStop(function () {
      console.log('onBackgroundAudioStop')
    })

  },//播放音乐
  listenerButtonPlay: function () {
    console.log("音乐播放");
    this.setData({
      mp3: 1
    })
    wx.playBackgroundAudio({
      //播放地址
      dataUrl: 'https://apitest.cndzys.com/wzf/1.mp3',
      title: '青云志',
      //图片地址
      coverImgUrl: 'http://r1.ykimg.com/050E0000576B75F667BC3C136B06E4E7'

    })
  },
  /**
* 监听button暂停按钮
*/
  listenerButtonPause: function () {
    console.log("音乐暂停");
    this.setData({
      mp3: 0
    })

    wx.pauseBackgroundAudio();
  },


  fanhui:function() {
    console.log("这是点击了返回事件");
    var num = this.data.num -= 1;
    num=100-num;
    console.log('我点击了'+num+'下');
    
    var pics= this.data.pics;
    var pics_leng = this.data.pics.length;
    
    if (pics_leng - num <="0"){
      console.log('没有图片了');
    }
    else{
      var img_url=pics[num-1];
      var img_url1 = img_url.split("=====");
      var img_url = img_url1[0];
      var img_id = img_url1[1];
      console.log('img_id===' + img_id);
      console.log('img_url====' + img_url);

      
      this.setData({ img_url: img_url })
    }
    },

  share: function () {
    console.log("这里是分享的事件");
    var openid = getApp().globalData.openid;
    var nickname = getApp().globalData.nickName;
    var that = this;
    wx.request({
      url: getApp().globalData.API_URI + '/index.php/face_share',
      data: { //appid 与 secret为对应小程序项目后台获取
        'openid': openid,
        'nickname': nickname,
      },
      header: {//请求头  
        "Content-Type": "application/x-www-form-urlencoded"
      },
      method: 'POST',
      success: function (res) {
        console.log(res.data)
        that.setData({ jifen: res.data.jifen, })
      }
    })
  },
  save: function () {
    console.log("这里是保存的事件");
    var share_title = getApp().globalData.share_title;
    console.log("share_title===" + share_title);
    this.setData({ share_title: share_title })
    this.setData({ bcfx: "1"})
  },

  save_quxiao: function () {
    console.log("这里是保存取消的事件");
    this.setData({ bcfx: "2" })
  },
  save_baocun: function () {
    console.log("这里是保存的手机事件");
  },

  change: function () {
    this.setData({ num: "100", img_url11:"1" })
    var openid = getApp().globalData.openid;
    var nickname = getApp().globalData.nickName;
    var img_url = getApp().globalData.img_url;
    var people_type1 = getApp().globalData.people_type1;
    var that = this;
     wx.request({
      url: getApp().globalData.API_URI + '/index.php/face_change',
      data: { //appid 与 secret为对应小程序项目后台获取
        'openid': openid,
        'nickname': nickname,
        'people_type1': people_type1,
        'img_url': img_url,
      },
      header: {"Content-Type": "application/x-www-form-urlencoded"},
      method: 'POST',
      success: function (res) {
        if (res.data.cond=="1"){
          that.setData({ error: res.data.error, })
          that.setData({ error1: 1 })
          that.setData({ img_url11: 2 })
          setTimeout(function () {
            wx.navigateTo({url: '../index/index',})
            that.setData({ error1: 2 })
          }, 2000)
        }
        getApp().globalData.share_title = res.data.share_title;
        var pics = that.data.pics.concat(res.data.img_url + '=====' + res.data.face_id)
        that.setData({
          pics: pics
        })
        that.setData({ jifen: res.data.jifen, })
        that.setData({ img_url: res.data.img_url, })
        that.setData({ pics: pics })
        that.setData({ img_url11: "2" })
      }

    })
  },


	/**
	* 播放状态
	*/
  listenerButtonGetPlayState: function () {
    wx.getBackgroundAudioPlayerState({
      success: function (res) {
        console.log(res)
        //duration 总时长
        //currentPosition 当前播放位置
        //status 播放状态
        //downloadPercent 下载状况 100 即为100%
        //dataUrl 当前播放音乐地址
      }
    })
  },

	/**
	* 设置进度
	*/
  listenerButtonSeek: function () {
    wx.seekBackgroundAudio({
      position: 30
    })
  },
	/**
	*停止播放 
	*/
  listenerButtonStop: function () {
    wx.stopBackgroundAudio()
  }
})
